﻿using PortfolioFinalProject.DataModels;
using PortfolioFinalProject.Web.IDataAdapters.Adapters;
using PortfolioFinalProject.Web.IDataAdapters.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace PortfolioFinalProject.Web.Controllers
{
    [Authorize]
    public class ProjectController : ApiController
    {
        IPortfolioAdapter _adapter = new PortfolioAdapter();

        //GET api/project
        [AllowAnonymous]
        public IHttpActionResult GetAllProjects()
        {
            List<Project> projects = _adapter.GetAllProjects();
            return Ok(projects);
        }
        //POST api/project
        public IHttpActionResult PostNewProject([FromBody] Project project)
        {
            if (User.IsInRole("Admin"))
            {
                project = _adapter.AddProject(project);
                return Ok(project);
            }
            return Unauthorized();
        }
        //PUT api/project
        public IHttpActionResult PutProject([FromBody] Project project)
        {
            if (User.IsInRole("Admin"))
            {
                project = _adapter.UpdateProject(project);
                return Ok(project);
            }
            return Unauthorized();
        }
        //DELETE api/project/:id
        public IHttpActionResult DeleteProject(int id)
        {
            if (User.IsInRole("Admin"))
            {
                _adapter.DeleteProject(id);
                return Ok();
            }
            return Unauthorized();
        }
    }
}
